package com.burgeon.backend.Rest.Template.Reviews;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RetailData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindRetailInfo extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindRetailInfo(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */
    public RetailData getData(String orderId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(orderId), url);
        System.err.println(str);

        /*  String count = new JSONArray(str).getJSONObject(0).getString("count");*/

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        RetailData retailData = new RetailData();
        retailData.setDate(jsonArray.getJSONArray(0).getString(0));
        retailData.setCode(jsonArray.getJSONArray(0).getString(1));
        retailData.setIsSelfRai(jsonArray.getJSONArray(0).getString(2));
        retailData.setStore(jsonArray.getJSONArray(0).getString(3));
        retailData.setSinceDate(jsonArray.getJSONArray(0).getString(4));

        return retailData;
    }

    private MultiValueMap Data(String orderId) {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段


        columns.add("'YC_RETAIL_ID;BILLDATE'");//单据日期1
        columns.add("'YC_RETAIL_ID;DOCNO'");//单据编号2
        columns.add("'YC_RETAIL_ID;IS_SELFRAIS'");//是否自提

        columns.add("'C_STORE_ID;NAME'");//发货店仓名称

        columns.add("'YC_RETAIL_ID;SELFRAIS_DATE'");//自提日期

        params.put("columns", columns);
        params.put("table", "YC_RETAILITEM");//表名

        params.put("count", "true");


        HashMap paramss = new HashMap();

        paramss.put("column","'YC_RETAIL_ID;ID'");
        paramss.put("condition","'="+orderId+"'");

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindRetailInfo g = new FindRetailInfo("39.104.240.134:55321", "nea@burgeon.com.cn", "root55321");
        try {
            System.err.println(g.getData("147"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
